<?php
/**
 * Cache.php	cache factory
 *
 * @package		
 * @Copyright	(c) All rights reserved
 * @Author		skyCrack <skyCrack@126.com>
 * @Version		$Id$
 */

class Cache
{
	public static $availableFrontends	=	array('Core', 'Output', 'Class', 'File', 'Function', 'Page');
	public static $availableBackends	=	array('File', 'Sqlite', 'Memcached', 'APC');

	const CLEANING_MODE_ALL              = 'all';
	const CLEANING_MODE_OLD	             = 'old';
	const CLEANING_MODE_MATCHING_TAG	 = 'matchingTag';
	const CLEANING_MODE_NOT_MATCHING_TAG = 'notMatchingTag';

	static public function factory($frontend, $backend, $frontendOptions = array(), $backendOptions = array())
	{
		$frontend = @ucfirst($frontend);
		$backend = @ucfirst($backend);

		if ( !in_array($frontend, Cache::$availableFrontends) )
		{
			Cache::throwException("Incorrect frontend ($frontend)");
		}

		if ( !in_array($backend, Cache::$availableBackends) )
		{
			Cache::throwException("Incorrect backend ($backend)");
		}

		$frontendClass = 'Cache_' . ($frontend != 'Core' ? 'Frontend_' : '') . $frontend;
		$backendClass = 'Cache_Backend_' . $backend;

		require_once(str_replace('_', '/', $frontendClass) . '.php');
		require_once(str_replace('_', '/', $backendClass) . '.php');

		$frontendObject = new $frontendClass($frontendOptions);
		$backendObject = new $backendClass($backendOptions);
		$frontendObject->setBackend($backendObject);

		return $frontendObject;
	}

	public static function throwException($msg)
	{
		require_once 'Cache/Exception.php';
		throw Cache_Exception($msg);
	}
}
?>